/**
 *	(C) 2014 Yuanjj
 *
 *	define thread manager class
 *
 *	histroy
 * 		2014-08-07	yuanjj	- create the file
 */

#include "thread.h"
#include "errorcode.h"
#include "routine/global.h"

cls_thread::cls_thread()
{
    m_threads[TH_DB]	= &m_db_thread;
    m_threads[TH_NET]	= &m_net_thread;
    m_threads[TH_UI]	= &m_ui_thread;
}

cls_thread::~cls_thread()
{

}

/**
 *	initialize the thread objects
 *
 * 	return
 * 		==	PEM_OK		successful
 * 		!=	PEM_OK		errorcode
 */
pem_error_t cls_thread::instance()
{
    pem_error_t		res;
    pem_s32_t		i;

    for ( i = 0; i < TH_ALL; i++ )
    {
        res = m_threads[i]->start_thread();
        if ( PEM_OK != res )
        {
            g_log.write( LOG_ERR, "thread", "create thread(%u) failed[error = 0x%x]", i, res );
            break;
        }
    }

    return	res;
}

/**
 *	release the thread objects
 */
pem_void_t cls_thread::release()
{
    pem_s32_t		i;

    for ( i = 0; i < TH_ALL; i++ )
        m_threads[i]->stop_thread();
}
